package com.ruoyi.web.controller.module.platform;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.RegexUtil;
import com.ruoyi.system.domain.module.platform.PlatformHelp;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.platform.IPlatformHelpService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 帮助文档Controller
 *
 * @author ruoyi
 * @date 2021-10-30
 */
@Validated
@Api(value = "帮助文档控制器", tags = {"帮助文档管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/platform/help")
public class PlatformHelpController extends BaseController {

    private final IPlatformHelpService platformHelpService;

    /**
     * 查询帮助文档列表
     */
    @ApiOperation("查询帮助文档列表")
    @PreAuthorize("@ss.hasPermi('module:platformhelp:list')")
    @GetMapping("/list")
    public TableDataInfo<PlatformHelp> list(@Validated PlatformHelp platformHelp) {
        QueryWrapper<PlatformHelp> qw = new QueryWrapper<>();
        if(platformHelp.getParentId() !=null){
        	qw.eq("parent_id",platformHelp.getParentId());
		}
		qw.orderByAsc("sort");
        TableDataInfo<PlatformHelp> td = PageUtils.buildDataInfo(platformHelpService.page(PageUtils.buildPage(),qw));
        List<PlatformHelp> list = td.getRows();
        return td;
    }


	/**
	 * 获取部门下拉树列表
	 */
	@GetMapping("/treeSelect")
	public AjaxResult treeselect(PlatformHelp dept) {
		List<PlatformHelp> eventTypes = platformHelpService.getNoPageByList(dept);
		return AjaxResult.success(platformHelpService.buildPlatformHelpTreeSelect(eventTypes));
	}


	/**
     * 导出帮助文档列表
     */
    @ApiOperation("导出帮助文档列表")
    @PreAuthorize("@ss.hasPermi('module:platformhelp:export')")
    @Log(title = "帮助文档", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<PlatformHelp> export(@Validated PlatformHelp platformHelp) {
        List<PlatformHelp> list = platformHelpService.list();
        ExcelUtil<PlatformHelp> util = new ExcelUtil<>(PlatformHelp.class);
        return util.exportExcel(list, "帮助文档");
    }

    /**
     * 获取帮助文档详细信息
     */
    @ApiOperation("获取帮助文档详细信息")
    @PreAuthorize("@ss.hasPermi('module:platformhelp:query')")
    @GetMapping("/{id}")
    public AjaxResult<PlatformHelp> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        PlatformHelp platformHelp = platformHelpService.getById(id);
        return AjaxResult.success(platformHelp);
    }

    /**
     * 新增帮助文档
     */
    @ApiOperation("新增帮助文档")
    @PreAuthorize("@ss.hasPermi('module:platformhelp:add')")
    @Log(title = "帮助文档", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody PlatformHelp platformHelp) {

		platformHelp.setContent(RegexUtil.convertProductDescription(platformHelp.getContent()));
        return toAjax(platformHelpService.save(platformHelp) ? 1 : 0);
    }

    /**
     * 修改帮助文档
     */
    @ApiOperation("修改帮助文档")
    @PreAuthorize("@ss.hasPermi('module:platformhelp:edit')")
    @Log(title = "帮助文档", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody PlatformHelp platformHelp) {
		platformHelp.setContent(RegexUtil.convertProductDescription(platformHelp.getContent()));
        return toAjax(platformHelpService.updateById(platformHelp) ? 1 : 0);
    }

    /**
     * 删除帮助文档
     */
    @ApiOperation("删除帮助文档")
    @PreAuthorize("@ss.hasPermi('module:platformhelp:remove')")
    @Log(title = "帮助文档" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(platformHelpService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
